package com.team.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.dao.RoleMapper;
import com.team.entity.Role;
import com.team.service.RoleService;
import com.team.vo.Result;
import com.team.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WHZ
 * @date 2022年10月14日 11:07
 */
@Service
public class RoleServiceImpl implements RoleService {
    private int static1;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public Result delectRole(int id) {
        int i = roleMapper.deleteByPrimaryKey(id);
        return new Result(200,"删除成功",i);
    }

    /**
     * 修改角色描述
     * @param role
     * @return
     */
    @Override
    public Result updateRoleDescribe(Role role) {
        int i = roleMapper.updateByPrimaryKeySelective(role);
        return new Result(200,"修改成功",i);
    }

    /**
     * 添加角色
     * @param record
     * @return
     */
    @Override
    public Result insert(Role record) {
        int insert = roleMapper.insert(record);
        return new Result(200,"添加成功",insert);
    }

    /**
     * 获取全部角色
     * @return
     */
    @Override
    public Result selectAll() {
        List<Role> roles = roleMapper.selectAll();
        return new Result(200,"查询成功",roles);
    }

    /**
     * 获取全部角色并分页
     * @return
     */
    @Override
    public Result findByCondition(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        if(roleVo.getCreateTime()!=null){
            roleVo.setOpentime(roleVo.getCreateTime()[0]);
            roleVo.setEndtime(roleVo.getCreateTime()[1]);
        }

        PageHelper.startPage(currentPage,pageSize);
        List<Role> roles = roleMapper.selectByCondition(roleVo);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        return new Result(200,"查询成功",rolePageInfo);
    }

    /**
     * 发现通过管理员id
     *
     * @author 太阳偷渡青山
     * @param adminId 管理员id
     * @return {@link Result}
     */
    @Override
    public Result findByAdminId(Integer adminId) {
        //1.查询所有的角色
        List<Role> list= roleMapper.selectAll();
        //2.查询该员工具有的角色id
        List<String> roleIds= roleMapper.findRoleIdByAdminId(adminId);
        Map<String,Object> map=new HashMap<>(2);
        map.put("list",list);
        map.put("roleIds",roleIds);
        return new Result(2000,"查询成功",map);
    }

    /**
     * 编辑角色
     *
     * @author 太阳偷渡青山
     * @param adminId 管理员id
     * @param roleIds 角色id
     * @return {@link Result}
     */
    @Override
    public Result editRole(Integer adminId, Integer[] roleIds) {
        //1.根据用户删除该员工具有的角色id
        roleMapper.deleteByAdminId(adminId);
        //2.添加新的角色id
        roleMapper.insertRoleId(adminId,roleIds);
        return new Result(2000,"添加成功");
    }

    @Override
    public Result roleDescribe() {
        List<Role> roles = roleMapper.roleDescribe();
        return new Result(200,"查询成功",roles);
    }

}
